﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Net.Sockets;
using System.Net;
using System.Windows.Forms;

namespace Client.p2p
{
    public abstract class CommonCmdBackground
    {
        #region Events
        /// <summary>
        /// Occurs when a command received from a remote client.
        /// </summary>
        public event CommandReceivedEventHandler CommandReceived;
        /// <summary>
        /// Occurs when a command received from a remote client.
        /// </summary>
        /// <param name="e">The received command.</param>
        protected virtual void OnCommandReceived(CommandEventArgs e)
        {
            if (CommandReceived != null)
            {
                Control target = CommandReceived.Target as Control;
                if (target != null && target.InvokeRequired)
                    target.Invoke(CommandReceived, new object[] { this, e });
                else
                    CommandReceived(this, e);
            }
        }

        /// <summary>
        /// Occurs when a command had been sent to the the remote server Successfully.
        /// </summary>
        public event CommandSentEventHandler CommandSent;
        /// <summary>
        /// Occurs when a command had been sent to the the remote server Successfully.
        /// </summary>
        /// <param name="e">The sent command.</param>
        protected virtual void OnCommandSent(CommandEventArgs e)
        {
            if (CommandSent != null)
            {
                Control target = CommandSent.Target as Control;
                if (target != null && target.InvokeRequired)
                    target.Invoke(CommandSent, new object[] { this, e });
                else
                    CommandSent(this, e);
            }
        }

        /// <summary>
        /// Occurs when a command sending action had been failed.This is because disconnection or sending exception.
        /// </summary>
        public event CommandSendingFailedEventHandler CommandFailed;
        /// <summary>
        /// Occurs when a command sending action had been failed.This is because disconnection or sending exception.
        /// </summary>
        /// <param name="e">The sent command.</param>
        protected virtual void OnCommandFailed(CommandEventArgs e)
        {
            if (CommandFailed != null)
            {
                Control target = CommandFailed.Target as Control;
                if (target != null && target.InvokeRequired)
                    target.Invoke(CommandFailed, new object[] { this, e });
                else
                    CommandFailed(this, e);
            }
        }

        /// <summary>
        /// Occurs when the client disconnected.
        /// </summary>
        public event RemoteDisconnectedEventHandler RemoteDisconnected;
        /// <summary>
        /// Occurs when the server disconnected.
        /// </summary>
        /// <param name="e">Server information.</param>
        protected virtual void OnRemoteDisconnected(RemoteEventArgs e)
        {
            if (RemoteDisconnected != null)
            {
                Control target = RemoteDisconnected.Target as Control;
                if (target != null && target.InvokeRequired)
                    target.Invoke(RemoteDisconnected, new object[] { this, e });
                else
                    RemoteDisconnected(this, e);
            }
        }

        /// <summary>
        /// Occurs when this client disconnected from the remote server.
        /// </summary>
        public event DisconnectedFromRemoteEventHandler DisconnectedFromRemote;
        /// <summary>
        /// Occurs when this client disconnected from the remote server.
        /// </summary>
        /// <param name="e">EventArgs.</param>
        protected virtual void OnDisconnectedFromRemote(RemoteEventArgs e)
        {
            if (DisconnectedFromRemote != null)
            {
                Control target = DisconnectedFromRemote.Target as Control;
                if (target != null && target.InvokeRequired)
                    target.Invoke(DisconnectedFromRemote, new object[] { this, e });
                else
                    DisconnectedFromRemote(this, e);
            }
        }

        /// <summary>
        /// Occurs when this client connected to the remote server Successfully.
        /// </summary>
        public event ConnectingSuccessedEventHandler ConnectingSuccessed;
        /// <summary>
        /// Occurs when this client connected to the remote server Successfully.
        /// </summary>
        /// <param name="e">EventArgs.</param>
        protected virtual void OnConnectingSuccessed(ConnectEventArgs e)
        {
            if (ConnectingSuccessed != null)
            {
                Control target = ConnectingSuccessed.Target as Control;
                if (target != null && target.InvokeRequired)
                    target.Invoke(ConnectingSuccessed, new object[] { this, e });
                else
                    ConnectingSuccessed(this, e);
            }
        }

        /// <summary>
        /// Occurs when this client failed on connecting to server.
        /// </summary>
        public event ConnectingFailedEventHandler ConnectingFailed;
        /// <summary>
        /// Occurs when this client failed on connecting to server.
        /// </summary>
        /// <param name="e">EventArgs.</param>
        protected virtual void OnConnectingFailed(ConnectEventArgs e)
        {
            if (ConnectingFailed != null)
            {
                Control target = ConnectingFailed.Target as Control;
                if (target != null && target.InvokeRequired)
                    target.Invoke(ConnectingFailed, new object[] { this, e });
                else
                    ConnectingFailed(this, e);
            }
        }

        /// <summary>
        /// Occurs when the network had been failed.
        /// </summary>
        public event NetworkDeadEventHandler NetworkDead;
        /// <summary>
        /// Occurs when the network had been failed.
        /// </summary>
        /// <param name="e">EventArgs.</param>
        protected virtual void OnNetworkDead(EventArgs e)
        {
            if (NetworkDead != null)
            {
                Control target = NetworkDead.Target as Control;
                if (target != null && target.InvokeRequired)
                    target.Invoke(NetworkDead, new object[] { this, e });
                else
                    NetworkDead(this, e);
            }
        }

        /// <summary>
        /// Occurs when the network had been started to work.
        /// </summary>
        public event NetworkAlivedEventHandler NetworkAlived;
        /// <summary>
        /// Occurs when the network had been started to work.
        /// </summary>
        /// <param name="e">EventArgs.</param>
        protected virtual void OnNetworkAlived(EventArgs e)
        {
            if (NetworkAlived != null)
            {
                Control target = NetworkAlived.Target as Control;
                if (target != null && target.InvokeRequired)
                    target.Invoke(NetworkAlived, new object[] { this, e });
                else
                    NetworkAlived(this, e);
            }
        }

        #endregion

    }
}
